<?php
	require_once "header.php";

	$isSuccess = 0 ;
	$cFilter = new CFilter;

	if(isset($_POST['btn_ok'])  && isset ($_POST['hTypeAction']) )
	{
		require_once "trackingModify.php";
		$action = $_POST['hTypeAction'];
		
		if($action === "add")
		{
			
			$id_category = $_POST['lCaterogies'];
			$name = $cFilter -> filter ($_POST['tf_name']);
			
			$description = $_POST['tf_description'];
			
			if($name == "")
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=types&to=idbadd&showCategory='.$id_category.'&name='.$name.'&notification=TYPE_ACTION_ADD_ERROR_NAME"</script>';  
				return ;
			}
			
			if($id_category == -1)
			{

			
				 echo '<script language="javascript">window.location.href="main.php?jmp=types&to=idbadd&showCategory='.$id_category.'&name='.$name.'&notification=TYPE_ACTION_ADD_ERROR_CATEGORY"</script>';  
				 return ;
			}
			else
			{
				$type = new CType;
				$isSuccess = $type->CTypeAdd($id_category, $name, $description,$status=1);				
				
				if ($isSuccess <= 0)
				{
					echo '<script language="javascript">window.location.href="main.php?jmp=types&to=idbadd&showCategory='.$id_category.'&name='.$name.'&notification=XXXX_ACTION_TO_SERVER"</script>';  
					
				}
				else
				{
					echo '<script language="javascript">window.location.href="main.php?jmp=catetype&to=idbview&notification=TYPE_ACTION_ADD_SUCCESS"</script>';				
					return ;
				}	
				
			}		
		}
		
		if($action === 'edi')
		{
		
			
			$id_category = $_POST['lCategories'];
			$id = $_POST['showType'];
			$name = $cFilter -> filter ($_POST['tf_name']);
			$description = $_POST['tf_description'];
			$status = $_POST['rdbtn_status'];
			
			
			if($name == "")
			{
		
				echo '<script language="javascript">window.location.href="main.php?jmp=types&to=idbedi&idbpos='.$id.'&notification=TYPE_ACTION_EDIT_ERROR_NAME"</script>';  
				return ;
			}
			
			if($id_category ==-1)
			{
				//cho nay thieu thong bao ra ket qua --> la` nguoi dung` chua chon category
				 echo '<script language="javascript">window.location.href="main.php?jmp=types&to=idbedi&idbpos="'.$id.'&notification=TYPE_ACTION_EDIT_ERROR_CATEGORY"</script>';  
				 return ;
			}
			else
			{
				$type = new CType;
				$isSuccess = $type->CTypeUpdate($id,$id_category, $name, $description,$status);			
				
				if($isSuccess > 0)
					echo '<script language="javascript">window.location.href="main.php?jmp=catetype&to=idbview&notification=TYPE_ACTION_EDIT_SUCCESS"</script>';
				else
					echo   	'<script language="javascript">window.location.href="main.php?jmp=types&to=idbedi&idbpos='.$id.'&notification=XXXX_ACTION_TO_SERVER"</script>';
			}
		}
		
		///dang lam den day 
		if($action === 'del')
		{
			
			$id = $_POST['showType'];
			
			$cType = new CType;
			
			$cItems = new CItems;
			
			$arrItems = $cItems -> CItemsOfType($id);
			$numOfItems = 0 ;
			
			if(is_array($arrItems ))
				$numOfItems = count($arrItems);
			
			if($numOfItems <=0 )
			{
				$isSuccess = $cType->CTypeDelete($id);			
				echo '<script language="javascript">window.location.href="main.php?jmp=catetype&to=idbview&notification=TYPE_ACTION_DEL_SUCCESS"</script>'; 
				return ; 	
			}
			else
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=types&to=idbdel&idbpos='.$id.'&notification=TYPE_ACTION_DEL_ERROR"</script>';
				return;

			}	
							
		}
		
	}
	else
	{
		echo '<script language="javascript">window.location.href="main.php?jmp=catetype&to=idbview"</script>';				
	}
?>